<?php
/**
 * MerchantController is for the merchant handler.
 * 
 * LBB Solutions Ltd. 版权所有 @ 2013.
 */
class MerchantController extends MobileController
{
	/**
	 * return the filter configuration for this controller.
	 * @return type 
	 */
	public function filters() {
		// get cache string
		//$varyByExpression = date('Y-m');
        return array(
			'accessControl', // perform access control for CRUD operations
            array(
                'COutputCache + detail',    // Cache for detail view
                'duration' => 7* 24 * 3600,    // one week caching
				//'varyByExpression' => $varyByExpression,
				'varyByParam' => array('id', 'lang'),
				'requestTypes' => array('GET'),
            ),
        );
    }
	
	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules() {
		return array(
			array('allow', // allow authenticated user to perform below actions
				'actions' => array('following'),
				'users' => array('@'),
			),
			array('allow',  // allow all users to perform below actions
				'actions' => array('view','detail'),
				'users' => array('*'),
			),
			array('deny', // deny all users
				'users' => array('*'),
			),
		);
	}
	
	/**
	 * View merchant page.
	 */
	public function actionView()
	{
		$this->render('view');
	}
	
	/**
	 * get user's following merchants.
	 */
	public function actionFollowing()
	{
		if (Yii::app()->request->isAjaxRequest) {
			//fetch the data
			
		}
		
		if (Yii::app()->request->isAjaxRequest)
			$this->renderPartial('_following', array('rows'=>$rows));
		else $this->render('following');
	}
	
	/**
	 * This page is for mobile app.
	 */
	public function actionDetail()
	{
		// get id_merchant
		$id_merchant = Yii::app()->request->getParam('id', null);
		if (empty($id_merchant)) {
			throw new CHttpException(404, 'The requested page does not exist.');
		};
		
		$params = array(
			':id_merchant' => $id_merchant,
		);
		
		$sql_merchant = 'SELECT m.id, m.name, m.biz_area, m.url_site, m.currency_code, m.brief, m.category_key,
			t.cuisine_style, t.cuisine_type, t.meal_served, t.payment_options, t.service_languages, 
			t.alcohol_license, t.dining_ambience, t.car_parking, t.additional_details
			FROM tbl_merchant_account m
			LEFT JOIN tbl_merchant_tags t ON m.id=t.id_merchant
			WHERE m.password IS NOT NULL AND m.id=:id_merchant';
		$connection = Yii::app()->db;
		$command = $connection->createCommand($sql_merchant);
		$row = $command->queryRow(true, $params);
		
		//3. get meals : max = 6
		$sql_dishes = 'SELECT i.id, i.id_merchant, i.url_picture, i.name, i.price, i.price_flexible, i.featured, i.sensitive_code
			FROM tbl_merchant_menu_item i
			WHERE i.id_merchant=:id_merchant
			ORDER BY i.featured DESC, i.submitted_time DESC
			LIMIT 6';
		$command = $connection->createCommand($sql_dishes);
		$row['meals'] = $command->queryAll(true, array(':id_merchant'=>$id_merchant));
		
		//4. get merchant's open hours
		$sql_hours = 'SELECT week_no, open_hour, close_hour FROM tbl_merchant_open_hours
			WHERE id_merchant=:id_merchant ORDER BY week_no ASC';
		$command = $connection->createCommand($sql_hours);
		$row['hours'] = $command->queryAll(true, array(':id_merchant'=>$id_merchant));
		
		$connection->active = false;
		
		$this->layout = 'mobile-layout-nohead';
		$this->render('detail', array('model' => $row));
	}
}